package com.opencloud.system.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.opencloud.common.model.PageParams;
import com.opencloud.common.model.Result;
import com.opencloud.common.utils.StringUtils;
import com.opencloud.system.client.model.UserInfo;
import com.opencloud.system.client.model.entity.SystemRole;
import com.opencloud.system.client.model.entity.SystemUser;
import com.opencloud.system.client.service.ISystemUserServiceClient;
import com.opencloud.system.service.SystemRoleService;
import com.opencloud.system.service.SystemUserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * 系统用户信息
 *
 * @author liuyadu
 */
@Api(tags = "系统用户管理")
@RestController
public class SystemUserController implements ISystemUserServiceClient {
    @Autowired
    private SystemUserService systemUserService;
    @Autowired
    private SystemRoleService systemRoleService;


    @ApiOperation(value = "获取账号登录信息", notes = "仅限系统内部调用")
    @PostMapping("/user/login")
    @Override
    public Result<UserInfo> login(@RequestParam(value = "username") String username,@RequestParam(value = "thirdParty",required = false)  String thirdParty) {
        UserInfo account = systemUserService.login(username,thirdParty);
        return Result.ok().data(account);
    }


    @ApiOperation(value = "获取系统用户列表", notes = "获取系统用户列表")
    @GetMapping("/user")
    @Override
    public Result<Page<SystemUser>> getPage(@RequestParam Map<String, Object> map) {
        return Result.ok().data(systemUserService.findPage(new PageParams(map)));
    }

    @ApiOperation(value = "获取用户详情", notes = "获取用户详情")
    @GetMapping("/user/info")
    @Override
    public Result<SystemUser> get(Long userId) {
        return Result.ok().data(systemUserService.getById(userId));
    }

    @ApiOperation(value = "获取所有用户列表", notes = "获取所有用户列表")
    @GetMapping("/user/list")
    @Override
    public Result<List<SystemUser>> getList() {
        return Result.ok().data(systemUserService.findList());
    }

    @ApiOperation(value = "添加/修改系统用户", notes = "添加/修改系统用户")
    @PostMapping("/user/save")
    @Override
    public Result<Long> save(@RequestBody  SystemUser systemUser) {
        if (systemUser.getUserId() == null) {
            systemUserService.add(systemUser);
        } else {
            systemUserService.update(systemUser);
        }
        return Result.ok();
    }

    @ApiOperation(value = "修改用户密码", notes = "修改用户密码")
    @PostMapping("/user/update/password")
    @Override
    public Result updatePassword(@RequestParam(value = "userId") Long userId, @RequestParam(value = "password") String password) {
        systemUserService.updatePassword(userId, password);
        return Result.ok();
    }

    @ApiOperation(value = "用户分配角色", notes = "用户分配角色")
    @PostMapping("/user/save/roles")
    @Override
    public Result saveRoles(
            @RequestParam(value = "userId") Long userId,
            @RequestParam(value = "roleIds", required = false) String roleIds
    ) {
        systemRoleService.saveRoles(userId, StringUtils.isNotBlank(roleIds) ? roleIds.split(",") : new String[]{});
        return Result.ok();
    }

    @ApiOperation(value = "获取用户已分配角色", notes = "获取用户已分配角色")
    @GetMapping("/user/roles")
    @Override
    public Result<List<SystemRole>> getUserRoles(@RequestParam(value = "userId") Long userId) {
        return Result.ok().data(systemRoleService.findRoleIdsByUserId(userId));
    }

    @ApiOperation(value = "注册第三方系统登录账号", notes = "仅限系统内部调用")
    @PostMapping("/user/add/thirdParty")
    @Override
    public Result addThirdParty(
            @RequestParam(value = "account") String account,
            @RequestParam(value = "password") String password,
            @RequestParam(value = "accountType") String accountType,
            @RequestParam(value = "nickName") String nickName,
            @RequestParam(value = "avatar") String avatar
    ) {
        SystemUser user = new SystemUser();
        user.setNickName(nickName);
        user.setUserName(account);
        user.setPassword(password);
        user.setAvatar(avatar);
        systemUserService.addThirdParty(user, accountType);
        return Result.ok();
    }

}
